//
//  AbstractWriter.cpp
//  NSESolver
//
//  Created by Patrick Zulian on 5/9/11.
//  Copyright 2011 ICS - USI - Lugano. All rights reserved.
//


#include "AbstractWriter.h"






namespace nsesolver {
    
    
    AbstractWriter::AbstractWriter(const std::string &path, const std::string &extension, const CartesianTopology &cart)
    : path(path), extension(extension), cart(cart)
    { }
    
    AbstractWriter::~AbstractWriter()
    { }
    
 

    void AbstractWriter::solverEnded(const std::vector< double > &ts, const std::vector< double > &taus)
    {
        using std::ofstream;
        using std::string;
        using std::ios;
        
        if (!cart.getCommunicator().isRoot()) return;
        
        
        const string tsFile(path + "/ts" + extension);
        const string tausFile(path + "/taus" + extension);
        
        ofstream os;
        os.open(tsFile.c_str());
        if (os.good()) {
            Array< double > arr( ts.size(), &ts[0]);
            os << arr;
        }
        
        os.close();
        
        os.open(tausFile.c_str());
        if (os.good()) {
            Array< double > arr( ts.size(), &taus[0]);
            os << arr;
        }
        
        os.close();
        
    }
    
    
    
    void AbstractWriter::auxShift(Matrixd &mOut, const Matrixd &mIn, int jOffset, int iOffset) const
    {
        for (int i = iOffset; i < mIn.xmax(); i++) {
            for (int j = jOffset; j < mIn.ymax(); j++) {
                mOut(i-iOffset,j-jOffset) = (mIn(i-iOffset, j-jOffset) + mIn(i,j))/2.0;
            }
        }
    }
    
    
    std::string AbstractWriter::intToStr( int n )
    {
        std::ostringstream result;
        result << n;
        return result.str();
    }
    

    
}

